<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <el-form :model="proVal" label-width="100px" size="mini">
          <el-form-item :label="$t('market.consignDateSta')">
            <el-date-picker
              v-model="proVal.startDate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width: 175px"
            />
          </el-form-item>
          <el-form-item :label="$t('market.consignDateEnd')">
            <el-date-picker
              v-model="proVal.endDate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width: 175px"
            />
          </el-form-item>
          <el-form-item :label="$t('menu.apply')">
            <el-input v-model.trim="proVal.fbillno" />
          </el-form-item>
          <el-form-item :label="$t('market.paezText1')">
            <el-input v-model.trim="proVal.khddk" />
          </el-form-item>
          <el-form-item :label="$t('market.state')">
            <el-select v-model.trim="proVal.yq">
              <el-option label="全部" value="" />
              <el-option label="已逾期" value="已逾期" />
              <el-option label="正常" value="正常" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.deaprt')">
            <el-input v-model.trim="proVal.bm" />
          </el-form-item>
          <el-form-item :label="$t('shop.fname2')">
            <el-input v-model.trim="proVal.itemName" />
          </el-form-item>
          <el-form-item label-width="50px">
            <el-button type="primary" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
            <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="handleInto(col.scope.row.物料编号)">{{ col.scope.row.物料编号 }}</el-link>
        </template>
        <template v-slot:billSlot="col">
          <el-tag v-if="col.scope.row.状态==='正常'" type="primary">{{ col.scope.row.状态 }}</el-tag>
          <el-tag v-else type="danger">{{ col.scope.row.状态 }}</el-tag>
        </template>
        <!--领料状态-->
        <template v-slot:btnStates="col">
          <el-tag v-if="col.scope.row.是否领料 === '全部领料'" type="success">{{ $t('market.pickingAll') }}</el-tag>
          <el-tag v-else-if="col.scope.row.是否领料 === '部分领料'">{{ $t('market.pickingPart') }}</el-tag>
          <el-tag v-else type="danger">{{ $t('market.pickingNot') }}</el-tag>
        </template>
        <!--齐套-->
        <template v-slot:text3Slot="col">
          <el-tag v-if="col.scope.row.是否齐套 === '不齐套'" type="danger">{{ $t('market.fulls2') }}</el-tag>
          <el-tag v-else>{{ $t('market.fulls') }}</el-tag>
        </template>
      </jc-table>
      <!--    分页器-->
      <div class="footer">
        <jc-pagination
          :total="total"
          :page.sync="currentPage"
          :limit.sync="size"
          @pagination="handleGetUntreated"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryProduction
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel2'
import searData from '@/components/Search/mixin'
import picker from '@/components/Search/picker'

export default {
  mixins: [searData, picker],
  data() {
    return {
      proVal: {
        fbillno: '',
        itemType: '',
        itemName: '',
        bm: '',
        yq: '',
        khddk: '',
        startDate: '',
        endDate: ''
      },
      currentPage: 1,
      size: 200,
      total: 0,
      isDetailDialog: false,
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.order'), prop: '销售单号', width: '120' },
        { label: this.$t('market.placedate'), prop: '下单日期' },
        { label: this.$t('market.consignDate'), prop: '交货日期' },
        { label: this.$t('market.salKhddh'), prop: '客户单号', width: '120' },
        { label: this.$t('market.supplier'), prop: '供应商', width: '150' },
        { label: this.$t('market.documentstatus'), prop: '审核状态' },
        { label: this.$t('shop.state'), type: 'bill', prop: '状态' },
        { label: this.$t('market.productmolde'), prop: '产品型号' },
        { label: this.$t('market.fmaterial'), type: 'tag', prop: '物料编号', width: '120' },
        { label: this.$t('shop.fname2'), prop: '物料名称' },
        { label: this.$t('market.specificaTion'), prop: '尺寸规格' },
        { label: this.$t('market.fdescripTion'), prop: '物料描述', width: '200' },
        { label: this.$t('market.funit'), prop: '单位' },
        { label: this.$t('market.billType'), prop: '单据类型' },
        { label: this.$t('market.scdh'), prop: '生产单号' },
        { label: this.$t('market.prodept'), prop: '生产部门' },
        { label: this.$t('market.productNum'), prop: '生产数量' },
        { label: this.$t('market.scrks'), prop: '生产入库数量' },
        { label: this.$t('market.stockQty'), prop: '库存数量' },
        { label: this.$t('market.pickstatus'), type: 'states', prop: '是否领料' },
        { label: this.$t('market.fulls5'), type: 'text3', prop: '是否齐套' }
      ],
      tableHeader1: []
    }
  },
  mounted() {
    this.benyue() // mixins picker
    this.proVal.endDate = this.timers[1]
    this.proVal.startDate = this.timers[0]
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 220px)' : '73vh'
  },
  methods: {
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        ...this.proVal,
        pageNum: this.currentPage,
        pageSize: this.size
      }
      const { data: RES, code, message } = await queryProduction(DATA)
      if (code === 0) {
        this.tableData = RES.list
        this.total = RES.total
      } else {
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 物料详情
    handleInto(ev) {
      this.$router.push({ path: `/detailMateriel/${ev}` })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          const DATA = {
            ...this.proVal,
            pageSize: 100000,
            pageNum: 1
          }
          queryProduction(DATA).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '生产进度一览表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  //@include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 12px;
        text-align: center;
      }
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 275px;
        margin-bottom: 5px;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
